package spindles.api.domain;

import static com.sleepycat.persist.model.DeleteAction.CASCADE;
import static com.sleepycat.persist.model.Relationship.MANY_TO_ONE;

import java.util.Date;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.time.DateFormatUtils;

import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.SecondaryKey;

@Entity
public class SleepSession extends DomainObject {	
	
	/**
	 * 
	 */
	private static transient final long serialVersionUID = -7040771144971870121L;

	private Date date;	
	
	@SecondaryKey(relate=MANY_TO_ONE)
	private int samplingRate;
	
	@SecondaryKey(relate=MANY_TO_ONE, 
				  relatedEntity=Person.class,
				  onRelatedEntityDelete=CASCADE)
	private long personID;	
	
	public SleepSession(){
		super();
	}
	
	public SleepSession(Date date, SamplingRate samplingRate, long personID){
		super();
		this.date = date;
		this.samplingRate = samplingRate.value();
		this.personID = personID;
	}
	
	public boolean equals(Object obj) {
		return 
			EqualsBuilder.reflectionEquals(this, obj, false, SleepSession.class);
	}
	
	public int hashCode() {		
		return HashCodeBuilder.reflectionHashCode(17, 
				37, this, false, SleepSession.class);	
	}


	public Date getDate() {
		return date;
	}
	
	public String getDateFormatted(){
		return DateFormatUtils.ISO_DATE_FORMAT.format(getDate());
	}


	public void setDate(Date date) {
		this.date = date;
	}


	public int getSamplingRate() {
		return samplingRate;
	}


	public void setSamplingRate(SamplingRate samplingRate) {
		this.samplingRate = samplingRate.value();
	}


	public long getPersonID() {
		return personID;
	}


	public void setPersonID(long personID) {
		this.personID = personID;
	}
	
	

}
